using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;

using phorm;
using phorm.config;
using phorm.database.core;
using phorm.database.mssql;
using phorm.model.fields;

namespace phorm.utils
{
    /// <summary>
    /// Utilities for an application that uses Microsoft SQL Server.
    /// </summary>
    public class MSSQLUtils
    {
	    /// <summary>
	    /// Create the database based on the config database name.
	    /// </summary>
        public static void createDatabase()
	    {
            MSSQLDatabase.connection();
            MSSQLDatabase.nonQuery(CRUD.createDatabase(PhormConfig.phormParams.dbName));
		    MSSQLDatabase.finish();
	    }

	    /// <summary>
	    /// Create all tables from the phorm base package.
	    /// </summary>
        public static void createTables()
	    {
            MSSQLDatabase.connection();

            Assembly asm = Assembly.GetExecutingAssembly();
            Type[] typelist = asm.GetTypes();
            List<BaseManyToMany> manyToManyList = new List<BaseManyToMany>();
            foreach (Type type in typelist)
            {
                if (!type.Namespace.Equals("phorm"))
                    continue;

                Object obj = Activator.CreateInstance(type) as Object;
                if (obj is MSSQLRecord)
                {
                    MSSQLDatabase.nonQuery((obj as MSSQLRecord).create());

                    //Make a list of many-to-many columns that will need to be built.
                    foreach (KeyValuePair<string, DatabaseColumn> column in (obj as MSSQLRecord).columns)
                        if (column.Value is BaseManyToMany)
                            manyToManyList.Add(column.Value as BaseManyToMany);
                }
            }

            //Create many-to-manys after all other tables are built.
            foreach (BaseManyToMany manyToMany in manyToManyList)
            {
                MSSQLDatabase.nonQuery(manyToMany.create());
            }

		    MSSQLDatabase.finish();
	    }

	    /// <summary>
        /// Empty all tables from the phorm base package.
	    /// </summary>
        public static void emptyTables()
	    {
            MSSQLDatabase.connection();
            Assembly asm = Assembly.GetExecutingAssembly(); ;
            Type[] typelist = asm.GetTypes();
            foreach (Type type in typelist)
            {
                if (!type.Namespace.Equals("phorm"))
                    continue;

                Object obj = Activator.CreateInstance(type) as Object;
                if (obj is MSSQLRecord)
                {
                    MSSQLDatabase.nonQuery((obj as MSSQLRecord).empty());
                }
		    }
		    MSSQLDatabase.finish();
	    }

	    /// <summary>
        /// Drop all tables from the phorm base package.
	    /// </summary>
        public static void dropTables()
	    {
            MSSQLDatabase.connection();
            Assembly asm = Assembly.GetExecutingAssembly(); ;
            Type[] typelist = asm.GetTypes();
            foreach (Type type in typelist)
            {
                if (!type.Namespace.Equals("phorm"))
                    continue;

                Object obj = Activator.CreateInstance(type) as Object;
                if (obj is MSSQLRecord)
                {
                    MSSQLDatabase.nonQuery((obj as MSSQLRecord).drop());
                }
            }
            MSSQLDatabase.finish();
	    }
    }
}
